package com.ruoyi.business.processIou.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.bizsys.domain.LoanIou;
import com.ruoyi.bizsys.mapper.LoanIouMapper;
import com.ruoyi.business.processIou.domain.LoanIouChangeRecord;
import com.ruoyi.business.processIou.mapper.ILoanIouChangeRecordMapper;
import com.ruoyi.business.processIou.service.ILoanIouChangeRecordService;
import com.ruoyi.bizsys.utils.BizConstants;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author wqg
 * @description
 * @date 2022/3/25
 */
@Service
public class ILoanIouChangeRecordImpl extends ServiceImpl<ILoanIouChangeRecordMapper, LoanIouChangeRecord> implements ILoanIouChangeRecordService {

    @Autowired
    ILoanIouChangeRecordMapper loanIouChangeRecordMapper;
    @Autowired
    LoanIouMapper loanIouMapper;

    /**
     *
     * @param loanIouOrg 原来的数据
     * @param lateFeePlus 剩余罚息
     * @param lateFeePlusAll 总罚息
     * @param loanIou 新的数据
     */
    @Override
    // @Transactional 外层使用了futrue.get 不能事物
    public void updateAndCreateRecord(LoanIou loanIouOrg, BigDecimal lateFeePlus , BigDecimal lateFeePlusAll, LoanIou loanIou) {

        // 记录之前的数据
        LoanIouChangeRecord loanIouChangeRecordBefro = new LoanIouChangeRecord();
        BeanUtils.copyProperties(loanIouOrg,loanIouChangeRecordBefro);
        processProperties(loanIouChangeRecordBefro,loanIouOrg,LoanIouChangeRecord.RECORD_TYPE_BEFOR);
        // loanIouChangeRecordMapper.insert(loanIouChangeRecordBefro);

        // 记录变更之后的数据
        LoanIouChangeRecord loanIouChangeRecordAfter = new LoanIouChangeRecord();
        processNewIou(loanIouOrg,loanIou,lateFeePlus,lateFeePlusAll);
        BeanUtils.copyProperties(loanIou,loanIouChangeRecordAfter);
        processProperties(loanIouChangeRecordAfter,loanIou,LoanIouChangeRecord.RECORD_TYPE_AFTER);
        loanIouMapper.updateById(loanIou);


    }

    /**
     * 计算新的数据
     * @param loanIouOrg
     * @param loanIou
     * @param lateFeePlusAll
     */
    private void processNewIou(LoanIou loanIouOrg, LoanIou loanIou, BigDecimal lateFeePlus,BigDecimal lateFeePlusAll) {
        // 最新的总罚息
        loanIou.setLateFee(lateFeePlusAll);
        // 已还罚息
        loanIou.setRepaidLateFee(lateFeePlusAll.subtract(lateFeePlus));
        // 已还总利息
        loanIou.setRepaidInterest(loanIouOrg.getInterest().subtract(loanIou.getInterest()));
        // 总利息
        loanIou.setLoanInterest(loanIouOrg.getLoanInterest());
        loanIou.setInterest(loanIouOrg.getInterest());
        // 已还服务费

        loanIou.setRepaidServiceFee(loanIouOrg.getServiceFee().subtract(loanIou.getServiceFee()));
        // 总服务费
        loanIou.setServiceFee(loanIouOrg.getServiceFee());
        // 已还本金
        loanIou.setRepaidPrincipal(loanIouOrg.getLoanAmt().subtract(loanIou.getLoanAmt()));
        // 本金
        loanIou.setLoanPrincipal(loanIouOrg.getLoanAmt());
        loanIou.setLoanAmt(loanIouOrg.getLoanAmt());
        //等于0，则b1=b2；
        //等于 1 ，则b1>b2 ;
        //等于-1，则b1<b2。
        loanIou.setFeeRemissionAmt(BigDecimal.ZERO);// 正常回溯不需要这个数据
        loanIou.setOverdueAmount(LoanIou.calculationOverdueAmount(loanIou));

    }
    /**
     * 处理对象参数
     * @param loanIouChangeRecord
     * @param loanIouOrg
     */
    private void processProperties(LoanIouChangeRecord loanIouChangeRecord, LoanIou loanIouOrg, int recordType) {
        loanIouChangeRecord.setId(null);
        loanIouChangeRecord.setIouId(loanIouOrg.getId());
        loanIouChangeRecord.setRecordType(recordType);
        loanIouChangeRecord.setRecordTime(new Date());
    }
}
